﻿/// <reference path="../general.js" />

function DoView() {
    document.forms[0].action = basePath + 'LichLamViec/DuyetLich/';
    document.forms[0].method = "post";
    document.forms[0].submit();
}

function DoGhiNhan() {
    document.forms[0].action = basePath + 'LichLamViec/DoDuyetLich/';
    document.forms[0].method = "post";
    document.forms[0].submit();
}

function CheckSingles(idCheckAll, nameCheckSingle, formId) {

    var isCheckAll = true;

    for (var i = 0; i < document.forms[formId].length; i++) {
        var item = document.forms[formId].elements[i];
        if (item.name == nameCheckSingle && item.type == 'checkbox' && item.checked == false) {
            isCheckAll = false;
            break;
        }
    }

    if (isCheckAll) {
        document.getElementById(idCheckAll).checked = true;
    } else {
        document.getElementById(idCheckAll).checked = false;
    }
}

function DoDuyetAllChuaDuyet() {
    CheckAlls("CheckAllChuaDuyet", "chkCheckChuaDuyet", "form");
    $("#0").addClass("title");
        if ($("#0").hasClass("title") == false) {
            $("#0").addClass("title");
        }
//    var ListId = "";
//    for (var z = 0; z < document.forms["form"].length; z++) {
//        var item = document.forms["form"].elements[z];
//        if (item.name == 'chkCheckChuaDuyet' && item.type == 'checkbox') {
//            ListId += item.value + ",";
//        }
//    }
//    $("#inListIdDuyet").val(ListId);
//    $("#inListIdChuaDuyet").val("");
//    var arr = ListId.split(",");
//    for (var i = 0; i < arr.length - 1; i++) {
//        //$("#" + arr[i]).remove();
//        DoDuyetChuaDuyet(arr[i]);
//    }
    
    //document.getElementById("CheckAllChuaDuyet").checked = false;
    //$("#0").addClass("title");
//    if ($("#0").hasClass("title") == false) {
//        $("#0").addClass("title");
//    }
}

//checkAllChuaDuyet
//$('#CheckAllChuaDuyet').click(function () {
//    if ($(this).prop('checked')) {
//        $('#tblChuaDuyet td>input:checkbox').each(function () {
//            $(this).prop('checked', true);
//        });
//    } else $('#tblChuaDuyet td>input:checkbox').each(function () {
//        $(this).prop('checked', false);
//    });
//    $("#0").addClass("title");
//    if ($("#0").hasClass("title") == false) {
//        $("#0").addClass("title");
//    }
//});

    //Do Checkbok chua duyet
function DoChkChuaDuyet() {
//    $('#tblChuaDuyet td>input:checkbox:checked').each(function () {
//        DoDuyetChuaDuyet($(this).attr("value"));
//    });
    $("input[id^='chkCheckChuaDuyet']:checked").each(function () {
        DoDuyetChuaDuyet($(this).attr("value"));
    });
    $('#CheckAllChuaDuyet').prop('checked', false);
}

function DoDuyetChuaDuyet(IdLich) {
    var ListIdChua = $("#inListIdChuaDuyet").val();
    var ListIdDuyet = $("#inListIdDuyet").val();
    ListIdDuyet += IdLich + ",";
    ListIdChua = ListIdChua.replace(IdLich + ",", "");
    $("#inListIdDuyet").val(ListIdDuyet);
    $("#inListIdChuaDuyet").val(ListIdChua);
    var trlich = document.getElementById(IdLich).innerHTML;
    trlich = trlich.replaceAll("ChuaDuyet", "DaDuyet");
    var trBottom = document.getElementById("border_bottom_DaDuyet").innerHTML;
    $("#border_bottom_DaDuyet").remove();
    $("#" + IdLich).remove();
    $("#tblDaDuyet>tbody").append("<tr class='item_row' onclick='javascript:highlight(this);' id='" + IdLich + "'>" + trlich + "</tr>");
    $("#tblDaDuyet>tbody").append("<tr class='border_bottom' id='border_bottom_DaDuyet'>" + trBottom + "</tr>");
}

//phần cho list dẵ duyêt
function DoDuyetAllDaDuyet() {
    CheckAlls("CheckAllDaDuyet", "chkCheckDaDuyet", "form");
    $("#0").addClass("title");
            if ($("#-1").hasClass("title") == false) {
                $("#-1").addClass("title");
            }
//    var ListId = "";
//    for (var z = 0; z < document.forms["form"].length; z++) {
//        var item = document.forms["form"].elements[z];
//        if (item.name == 'chkCheckDaDuyet' && item.type == 'checkbox') {
//            ListId += item.value + ",";
//        }
//    }
//    $("#inListIdChuaDuyet").val(ListId);
//    $("#inListIdDuyet").val("");
//    var arr = ListId.split(",");
//    for (var i = 0; i < arr.length - 1; i++) {
//        //$("#" + arr[i]).remove();
//        DoDuyetDaDuyet(arr[i]);
//    }

   
    //document.getElementById("CheckAllChuaDuyet").checked = false;
    //$("#0").addClass("title");
//    if ($("#-1").hasClass("title") == false) {
//        $("#-1").addClass("title");
//    }
}

//checkAllDaDuyet
//$('#CheckAllDaDuyet').click(function () {
//    if ($(this).prop('checked')) {
//        $('#tblDaDuyet td>input:checkbox').each(function () {
//            $(this).prop('checked', true);
//        });
//    } else $('#tblDaDuyet td>input:checkbox').each(function () {
//        $(this).prop('checked', false);
//    });
//    $("#0").addClass("title");
//        if ($("#-1").hasClass("title") == false) {
//            $("#-1").addClass("title");
//        }
//});

//Do Checkbok chua duyet
function DoChkDaDuyet() {
//    $('#tblDaDuyet td>input:checkbox:checked').each(function () {
//        DoDuyetDaDuyet($(this).attr("value"));
//    });
    $("input[id^='chkCheckDaDuyet']:checked").each(function () {
        DoDuyetDaDuyet($(this).attr("value"));
    });
    $('#CheckAllDaDuyet').prop('checked', false);
}

function DoDuyetDaDuyet(IdLich) {
    var ListIdChua = $("#inListIdChuaDuyet").val();
    var ListIdDuyet = $("#inListIdDuyet").val();
    ListIdChua += IdLich + ",";
    ListIdDuyet = ListIdDuyet.replace(IdLich + ",", "");
    $("#inListIdDuyet").val(ListIdDuyet);
    $("#inListIdChuaDuyet").val(ListIdChua);
    var trlich = document.getElementById(IdLich).innerHTML;
    trlich = trlich.replaceAll("DaDuyet", "ChuaDuyet");
    var trBottom = document.getElementById("border_bottom_ChuaDuyet").innerHTML;
    $("#border_bottom_ChuaDuyet").remove();
    $("#" + IdLich).remove();
    $("#tblChuaDuyet>tbody").append("<tr class='item_row' onclick='javascript:highlight(this);' id='" + IdLich + "'>" + trlich + "</tr>");
    $("#tblChuaDuyet>tbody").append("<tr class='border_bottom' id='border_bottom_ChuaDuyet'>" + trBottom + "</tr>");
}

//phần cho lightbox
$(document).ready(function () {
   
    $(".XemNguoiChuanBi").click(function () {
        var id = this.id.replace("XemNguoiChuanBi", "");
        var url = basePath + 'LichLamViec/ViewDetailNguoiChuanBi?IdLich=' + id;
        $("#tbl_DanhSachNguoiChuanBi>table>tbody").empty();
        $.getJSON(url, null, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#tbl_DanhSachNguoiChuanBi>table>tbody").append("<tr>" +
                                                            "<td style='width: 30px;text-align:right'><img src='../../Content/ImagesAdmin/check.PNG'/></td>" +
                                                            "<td>" + data[i] + "</td>" +
                                                       "</tr>");
            }
        });
        var p = $(this).position();
        $("#ViewNguoiChuanBi").css({ "top": +(p.top) + "px", "left": +(p.left - 270) + "px", "display": "block" });
    });

    $(".XemThanhPhan").click(function () {
        var id = this.id.replace("XemThanhPhan", "");
        $("#tbl_DanhSachThanhPhan>table>tbody").empty();
        var url = basePath + 'LichLamViec/ViewDetailThanhPhan?IdLich=' + id;
        $.getJSON(url, null, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#tbl_DanhSachThanhPhan>table>tbody").append("<tr>" +
                                                            "<td style='width: 30px;text-align:right'><img src='../../Content/ImagesAdmin/check.PNG'/></td>" +
                                                            "<td>" + data[i] + "</td>" +
                                                       "</tr>");
            }
        });
        var p = $(this).position();
        $("#ViewThanhPhan").css({ "top": +(p.top) + "px", "left": +(p.left - 270) + "px", "display": "block" });
    });
});

String.prototype.replaceAll = function (
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
}